﻿
/// <reference path="Component.js" />
/// <reference path="_namespace.js" />
/// <reference path="IMenuItemContainer.js" />
/// <reference path="../events/EventRouter.js" />

/*****************************************************************************
Ajax library class definition. Kevin Robinson 2006.
Class: ajax.components.MenuItemContainer
/*****************************************************************************/


ajax.lang.addNamespace("ajax.components");

//-----------------------------------------------------------------------------
ajax.components.MenuItemContainer = function(doc, parentObject, parentNode) // Constructor
{        
    ajax.components.MenuItemContainer.superConstructor.call(this, doc);
    
    var self = this;
    
    this.parentObject   = parentObject;
    this.parentNode     = parentNode;
    this.menuItems      = new Array();  // Holds the menu items 
    
    // Create an event router that clients can subscribe to
    this.getEvent(this.window, "onItemSelected");
    
    
    
    // Create an event delegate we will use to marshal Item events for our clients
    this.onItemSelectedDelegate = function(args)
    {
        var newArgs =
        {
            menuItem: this,
            eventArguments: args
        };
        self.onItemSelected.call(self, newArgs);
    };
}

//-----------------------------------------------------------------------------
// We Inherit From Component
ajax.lang.extend("ajax.components.MenuItemContainer", "ajax.components.Component");
// Now say that we support this interface
ajax.lang.supports("ajax.components.MenuItemContainer", "ajax.components.IMenuItemContainer");
if ($getDesignMode())
    ajax.components.MenuItemContainer.prototype = ajax.components.Component.prototype;

// Methods
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Support Interface
ajax.components.MenuItemContainer.prototype.add_itemSelected      = function(handler) { return this.getEvent(this.getWindow(), "onItemSelected").addListener(handler); };
ajax.components.MenuItemContainer.prototype.remove_itemSelected   = function(handler) { return this.getEvent(this.getWindow(), "onItemSelected").removeListener(handler); };

ajax.components.MenuItemContainer.prototype.getMenuItem = function(sKey)
{
    for (var i = 0; i < this.menuItems.length; i++)
    {
        if (this.menuItems[i].key == sKey)
            return this.menuItems[i];
    }
    return null;
}

ajax.components.MenuItemContainer.prototype.removeMenuItem = function(menuItem)
{
    // remove from the GUI
    this.parentNode.removeChild(menuItem.element);
    // Unsubscribe our events
    menuItem.getEvent(this.window, "onSelected").removeListener(this.onItemSelectedDelegate);
    // Remove from our array
    this.menuItems.remove(menuItem);
}

ajax.components.MenuItemContainer.prototype.addMenuItem = function(menuItem)
{
    var self = this;
    
    // Tell the menuItem that we are it's parent (just in case it cares)
    menuItem.menuItemContainer = this;
    
    // Append this Menu item to our menu items array
    this.menuItems.append(menuItem, false);
    
    // Sunscribe to the menu items onSelected event
    menuItem.getEvent(this.window, "onSelected").addListener(this.onItemSelectedDelegate);
    
    // Attach the div to the content panel
    this.parentNode.appendChild(menuItem.element);    
}



//-----------------------------------------------------------------------------
ajax.components.MenuItemContainer.prototype.toString = function()
{
    return "ajax.components.MenuItemContainer";
}

//-----------------------------------------------------------------------------
ajax.components.MenuItemContainer.prototype.toClassName = function()
{
    return ajax.lang.getClassName("ajax.components.MenuItemContainer");
}

//-----------------------------------------------------------------------------
ajax.components.MenuItemContainer.cast = function(obj)
{///<returns type="ajax.components.MenuItemContainer"/>
    return obj;
}